# This file is part of trytond-pcw-subproduct module for Tryton. 
# The COPYRIGHT file at the top level of this repository contains 
# the full copyright notices and license terms.

from trytond.model import ModelView, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pool import Pool, PoolMeta

__all__ = ['Subproduct']
__metaclass__ = PoolMeta

class Subproduct(ModelSQL, ModelView):
    "Subproduct"
    __name__ = 'pcw.subproduct'

    parent = fields.Many2One('product.product', 'Parent Product', ondelete='CASCADE', required=True)
    quantity = fields.Float( 'Quantity', required=True, digits=(16,4))
    uom = fields.Many2One('product.uom', 'UOM', required=True,
        domain=[('category', '=', Eval('product_uom_category'))],
        depends=['product_uom_category'])
    product = fields.Many2One('product.product', 'Product', required=True)
    product_uom_category = fields.Function(
        fields.Many2One('product.uom.category', 'Product Uom Category',
            on_change_with=['product']), 'on_change_with_product_uom_category')

    def on_change_with_product_uom_category(self, name=None):
        if self.product:
            return self.product.default_uom_category.id

